import { localCache } from '@/utils';

import HYRequest from './request';
import { BASE_URL, TIME_OUT } from './request/config';

// const AxiosMockAdapter = require('axios-mock-adapter');

const hyRequest = new HYRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config) => {
      const token = localCache.getCache('token');
      if (token) {
        config.headers.Authorization = `Bearer ${token}`;
      }
      return config;
    },

    requestInterceptorCatch: (err) => {
      console.log('请求失败的拦截');
      return err;
    },
    responseInterceptor: (res) => {
      console.log('响应成功的拦截');
      return res;
    },
    responseInterceptorCatch: (err) => {
      console.log('响应失败的拦截');
      return err;
    }
  }
});

// const mock = new AxiosMockAdapter(hyRequest);
// mock.onGet('/users', { params: { searchText: 'John' } }).reply(200, {
//   users: [{ id: 1, name: 'John Smith' }]
// });

export default hyRequest;
